﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMVC.Areas.AdminPanel.Models;
using System.IO;
using BusinessTier.Facade.Interface;
using BusinessTier.Facade.Implementation;
using Domain;
using WebMVC.Helpers;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class DashboardController : AdminControllerBase
    {
        private ICounterService counterService;
        

        public DashboardController() : this(new CounterService())
        {
            
        }

        public DashboardController(ICounterService counterService)
        {
            this.counterService = counterService;
        }

        public ActionResult Index()
        {
            return View();
        }


        [ChildActionOnly]
        public ActionResult UserStatistic()
        {
            var model = new UserStatisticViewModel()
            {
                RegisteredInPast24 = 0,
                RegisteredToday = 0,
                TotalApprovedUsers = 0,
                TotalUsers = 0,
                UsersOnline = 0
            };
            return PartialView(model);
        }

        [ChildActionOnly]
        public ActionResult SystemState()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            DriveInfo mainDrive = null;
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady && drive.Name.ToLower().Equals("c:\\"))
                {
                    mainDrive = drive;
                    break;
                }
            }

            var model = new SystemStateViewModel()
            {
                FreeDiskSpaceMB = mainDrive.TotalFreeSpace / (1024 * 1024),
                TotalDiskSpaceMB = mainDrive.TotalSize / (1024 * 1024)
            };
            return PartialView(model);
        }

        [ChildActionOnly]
        public ActionResult MonitoringStatus()
        {
            var model = new MonitoringViewModel()
            {
                SMTPServerStatus = ModuleStatus.OK,
                DataBaseStatus = ModuleStatus.OK,
            };
            return PartialView(model);
        }

        [ChildActionOnly]
        public ActionResult CounterStatistic()
        {
            
            return PartialView(counterService.GetLastCounterEntry());
        }

        public ActionResult Counters()
        {
            var counters = counterService.GetAllCounterEntries();
            return View(counters ?? new List<PrinterCounter>());
        }

        [HttpGet]
        [IPCheck]
        public ActionResult AddCounterEntry()
        {
            return View();
        }

        [HttpPost]
        [IPCheck]
        public ActionResult AddCounterEntry(PrinterCounter counter)
        {
            if (ModelState.IsValid)
            {
                counter.Date = DateTime.Now;
                counterService.AddPrintCounterEntry(counter);
                base.Info = "Counter entry for today added successfully";
                return RedirectToAction("Counters");
            }
            return View(counter);

        }
       
    }
}
